﻿using Infrastructure.Tool;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WsdShop.IService;
using WsdShop.IService.Common;
using WsdShop.Model;
using WsdShop.Model.SearchModels;
using WsdShop.Model.ViewModels;

namespace Admin.Controllers
{
    public class WxSetController : BaseController
    {
        protected IWxReplyService service;
        protected IWxImageTextService imgtextservice;
        protected IWxConfigService configservice;
        protected IWxMenuService wxmenuservice;
        protected ILogService logService;

        public WxSetController(IUserInfoService s, IWxReplyService _service, IWxImageTextService _imgtextservice, IWxConfigService _configservice, IWxMenuService _wxmenuservice,ILogService l) : base(s)
        {
            service = _service;
            imgtextservice = _imgtextservice;
            configservice = _configservice;
            wxmenuservice = _wxmenuservice;
            logService = l;
        }

        // GET: WxSet
        [AuthorizeFilter(MenuRole = "8")]
        [HttpGet]
        public ActionResult AutoReply()
        {
            return View();
        }

        /// <summary>
        /// 关键字自动回复
        /// </summary>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "8")]
        [HttpGet]
        public ActionResult KeyWordSelect()
        {
            return Json(service.KeyWordSelect());
        }

        /// <summary>
        /// 关注后自动回复
        /// </summary>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "8")]
        [HttpGet]
        public ActionResult ConcernSelect()
        {
            return Json(service.ConcernSelect());
        }

        /// <summary>
        /// 消息自动回复
        /// </summary>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "8")]
        [HttpGet]
        public ActionResult MessageSelect()
        {
            return Json(service.MessageSelect());
        }

        /// <summary>
        /// 删除关键字自动回复
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "8")]
        [HttpPost]
        public ActionResult DelKeyWordReply(int KeyWordId)
        {
            return Json(service.DelKeyWordReply(KeyWordId));
        }

        /// <summary>
        /// 添加关键字回复
        /// </summary>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "8")]
        [HttpPost]
        public ActionResult AddKeyWordReply()
        {
            return Json(service.AddKeyWordReply());
        }

        /// <summary>
        /// 更新关键字
        /// </summary>
        /// <param name="KeyWordId"></param>
        /// <param name="KeyWords"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "8")]
        [HttpPost]
        public ActionResult UpdateKeyWord(int KeyWordId, string KeyWords)
        {
            return Json(service.UpdateKeyWord(KeyWordId, KeyWords));
        }

        /// <summary>
        /// 更新回复内容
        /// </summary>
        /// <param name="KeyWordId"></param>
        /// <param name="ReplyContent"></param>
        /// <param name="ContentType"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "8")]
        [HttpPost]
        public ActionResult UpdateReplyContent(int KeyWordId, string ReplyContent, int ContentType = 0)
        {
            return Json(service.UpdateReplyContent(KeyWordId, ReplyContent, ContentType));
        }

        /// <summary>
        /// 更新规则名称
        /// </summary>
        /// <param name="KeyWordId"></param>
        /// <param name="RoleName"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "8")]
        [HttpPost]
        public ActionResult UpdateRoleName(int KeyWordId, string RoleName)
        {
            return Json(service.UpdateRoleName(KeyWordId, RoleName));
        }

        /// <summary>
        /// 获取所有图文素材
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetAllImageText()
        {
            return Json(imgtextservice.GetAllImageText());
        }

        /// <summary>
        /// 图文素材管理
        /// </summary>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "9")]
        [HttpGet]
        public ActionResult ImageTextManage()
        {
            return View();
        }

        /// <summary>
        /// 图文素材分页数据
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "9")]
        [HttpPost]
        public ActionResult ImageTextManage(ImageTextSearchModel search)
        {
            int total = 0;
            var list = imgtextservice.ImageTextManage(search, out total);
            return Json(new { Total = total, Results = list });
        }

        /// <summary>
        /// 获取素材
        /// </summary>
        /// <param name="GroupId"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "9")]
        [HttpPost]
        public ActionResult GetByGroupid(string GroupId)
        {
            return Json(imgtextservice.GetByGroupid(GroupId));
        }

        /// <summary>
        /// 删除素材
        /// </summary>
        /// <param name="GroupId"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "9")]
        [HttpPost]
        public ActionResult DeleteByGroupid(string GroupId)
        {
            return Json(imgtextservice.DeleteByGroupid(GroupId));
        }

        /// <summary>
        /// 图文素材编辑
        /// </summary>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "9")]
        [HttpGet]
        public ActionResult ImageTextEdit()
        {
            return View();
        }

        /// <summary>
        /// 删除子文章
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="GroupId"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "9")]
        [HttpPost]
        public ActionResult DeleteById(int Id, string GroupId)
        {
            return Json(imgtextservice.DeleteById(Id, GroupId));
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="ImageTexts"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "9")]
        [HttpPost]
        public ActionResult SaveAllImageText(IList<WxImageText> ImageTexts)
        {
            return Json(imgtextservice.SaveAllImageText(ImageTexts));
        }

        /// <summary>
        /// 微信配置
        /// </summary>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "7")]
        [HttpGet]
        public ActionResult WxConfig()
        {
            return View();
        }

        /// <summary>
        /// 微信配置数据获取
        /// </summary>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "7")]
        [HttpPost]
        public ActionResult GetConfig()
        {
            return Json(configservice.GetConfig());
        }

        /// <summary>
        /// 保存配置
        /// </summary>
        /// <param name="wxconfig"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "7")]
        [HttpPost]
        public ActionResult WxConfigSubmit(WxConfig wxconfig)
        {
            var result = configservice.WxConfigSubmit(wxconfig);
            if (result.isSuccess)
            {
                logService.OperationLog(CurrentLoginUser.Id, CurrentLoginUser.UserName, Request.RawUrl,
                            "修改微信配置", "修改成功", Generator.GetIP());
            }
            else
            {
                logService.OperationLog(CurrentLoginUser.Id, CurrentLoginUser.UserName, Request.RawUrl,
                           "修改微信配置", "修改失败", Generator.GetIP());
            }
            return Json(result);
        }

        /// <summary>
        /// 微信菜单
        /// </summary>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "10")]
        [HttpGet]
        public ActionResult WxMenu()
        {
            return View();
        }

        /// <summary>
        /// 菜单数据
        /// </summary>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "10")]
        [HttpPost]
        public ActionResult GetWxMenu()
        {
            return Json(wxmenuservice.GetWxMenu());
        }

        /// <summary>
        /// 菜单保存
        /// </summary>
        /// <param name="MenuList"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "10")]
        [HttpPost]
        public ActionResult SubmitMenu(List<ParentMenu> MenuList)
        {
            var result = wxmenuservice.SubmitMenu(MenuList);
            if (result.isSuccess)
            {
                logService.OperationLog(CurrentLoginUser.Id, CurrentLoginUser.UserName, Request.RawUrl,
                            "修改微信菜单", "修改成功", Generator.GetIP());
            }
            else
            {
                logService.OperationLog(CurrentLoginUser.Id, CurrentLoginUser.UserName, Request.RawUrl,
                            "修改微信菜单", "修改失败"+result.msg, Generator.GetIP());
            }
            return Json(result);
        }

        /// <summary>
        /// 生成菜单
        /// </summary>
        /// <param name="MenuList"></param>
        /// <returns></returns>
        [AuthorizeFilter(MenuRole = "10")]
        [HttpPost]
        public ActionResult GenerateMenu(List<ParentMenu> MenuList)
        {
            var result = wxmenuservice.GenerateMenu(MenuList);
            if (result.isSuccess)
            {
                logService.OperationLog(CurrentLoginUser.Id, CurrentLoginUser.UserName, Request.RawUrl,
                            "生成微信菜单", "生成成功", Generator.GetIP());
            }
            else
            {
                logService.OperationLog(CurrentLoginUser.Id, CurrentLoginUser.UserName, Request.RawUrl,
                            "生成微信菜单", "生成失败"+result.msg, Generator.GetIP());
            }
            return Json(wxmenuservice.GenerateMenu(MenuList));
        }

    }
}